package com.it.ems.mapper;

import com.it.ems.entity.ExpressOrder;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Delete;

import java.util.List;

@Mapper
public interface ExpressOrderMapper {
    ExpressOrder findById(Long id);
    
    @Select("SELECT id, order_number AS orderNumber, user_id, sender_name AS senderName, sender_phone AS senderPhone, sender_address AS senderAddress, " +
            "receiver_name AS receiverName, receiver_phone AS receiverPhone, receiver_address AS receiverAddress, receiver_id AS receiverId, " +
            "package_type AS packageType, weight, volume, price, status, " +
            "created_at AS createdAt, updated_at AS updatedAt " +
            "FROM express_orders " +
            "WHERE user_id = #{userId}")
    List<ExpressOrder> findByUserId(Long userId);
    
    @Select("SELECT id, order_number AS orderNumber, user_id, sender_name AS senderName, sender_phone AS senderPhone, sender_address AS senderAddress, " +
            "receiver_name AS receiverName, receiver_phone AS receiverPhone, receiver_address AS receiverAddress, receiver_id AS receiverId, " +
            "package_type AS packageType, weight, volume, price, status, " +
            "created_at AS createdAt, updated_at AS updatedAt " +
            "FROM express_orders " +
            "WHERE order_number = #{orderNumber}")
    List<ExpressOrder> findByOrderNumber(String orderNumber);
    
    int insert(ExpressOrder order);
    
    int update(ExpressOrder order);
    
    @Delete("DELETE FROM express_orders WHERE id = #{id}")
    int delete(Long id);
    
    @Delete("DELETE FROM express_orders WHERE user_id = #{userId}")
    int deleteByUserId(Long userId);

    @Select("SELECT COUNT(*) FROM express_orders WHERE DATE(created_at) = CURDATE()")
    int countTodayOrders();

    @Select("SELECT COUNT(*) FROM express_orders WHERE status = #{status}")
    int countOrdersByStatus(@Param("status") String status);

    @Select("SELECT id, order_number AS orderNumber, user_id, sender_name AS senderName, sender_phone AS senderPhone, sender_address AS senderAddress, " +
            "receiver_name AS receiverName, receiver_phone AS receiverPhone, receiver_address AS receiverAddress, receiver_id AS receiverId, " +
            "package_type AS packageType, weight, volume, price, status, " +
            "created_at AS createdAt, updated_at AS updatedAt " +
            "FROM express_orders " +
            "ORDER BY created_at DESC LIMIT #{limit}")
    List<ExpressOrder> findRecentOrders(@Param("limit") int limit);

    @Select("SELECT id, order_number AS orderNumber, user_id, sender_name AS senderName, sender_phone AS senderPhone, sender_address AS senderAddress, " +
            "receiver_name AS receiverName, receiver_phone AS receiverPhone, receiver_address AS receiverAddress, receiver_id AS receiverId, " +
            "package_type AS packageType, weight, volume, price, status, " +
            "created_at AS createdAt, updated_at AS updatedAt " +
            "FROM express_orders " +
            "ORDER BY created_at DESC")
    List<ExpressOrder> findAllOrders();

    @Select("SELECT eo.id, eo.order_number AS orderNumber, eo.user_id, eo.sender_name AS senderName, eo.sender_phone AS senderPhone, eo.sender_address AS senderAddress, " +
            "eo.receiver_name AS receiverName, eo.receiver_phone AS receiverPhone, eo.receiver_address AS receiverAddress, eo.receiver_id AS receiverId, " +
            "eo.package_type AS packageType, eo.weight, eo.volume, eo.price, eo.status, " +
            "eo.created_at AS createdAt, eo.updated_at AS updatedAt " +
            "FROM express_orders eo " +
            "WHERE eo.receiver_id = #{userId} ORDER BY eo.created_at DESC")
    List<ExpressOrder> findReceivedOrdersByUserId(@Param("userId") Long userId);

    @Select("SELECT id, order_number AS orderNumber, user_id, sender_name AS senderName, sender_phone AS senderPhone, sender_address AS senderAddress, " +
            "receiver_name AS receiverName, receiver_phone AS receiverPhone, receiver_address AS receiverAddress, receiver_id AS receiverId, " +
            "package_type AS packageType, weight, volume, price, status, " +
            "created_at AS createdAt, updated_at AS updatedAt " +
            "FROM express_orders " +
            "WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<ExpressOrder> findSentOrdersByUserId(@Param("userId") Long userId);
} 